
const Api = require('@/api/index').default

let userMenus = sessionStorage.getItem('User-Menus');
if(!userMenus || typeof userMenus == 'undefined' || userMenus == 'undefined'){
  userMenus = '[]';
}

const formatObj = (obj)=>{
    var arr = [];
    for(let i = 0,l=obj.length;i<l;i++){
        if(obj[i]['resourceType']['code'] == 0){ //如果资源是菜单类型
          var temp = { 
              hiddenFlag:obj[i]['hiddenFlag'],
              dropDownFlag:obj[i]['dropDownFlag'],
              url : obj[i]['url'],
              icon : obj[i]['icon'] || '',
              resourceName : obj[i]['resourceName'],
              children:[]
          };
          if(obj[i]['children'] && obj[i]['children'].length > 0){
              temp.children = formatObj(obj[i]['children']);
          }
          arr.push(temp);
        }
    }
    return arr;
}

export default{
    state: {
        menus:JSON.parse(userMenus)
    },
    mutations: {
        SET_MENUS: (state, menus) => {
            Object.assign(state.menus, menus);
        }
    },
    actions:{
        SET_MENUS({commit},menus){
    		sessionStorage.setItem('User-Menus', JSON.stringify(menus));
    		commit('SET_MENUS',menus);
        }        
    },
    getters: {
        menus: state => {
            return state.menus;
        }
    }
}